<template>
  <div class="page_main">
    <div class="search-box">
      <el-form :inline="true" size="mini" :model="searchForm" ref="searchForm">
        <search-form-box :show-more-btn="false">
          <template slot="showSearch">
            <el-form-item label="物品类型" prop="goods_type_id">
              <el-select v-model="searchForm.goods_type_id" placeholder="请选择物品类型" clearable filterable>
                <el-option v-for="item in goods_type_arr" :key="item.id" :value="item.id" :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="出库人：" prop="user_id">
              <el-select v-model="searchForm.user_id" placeholder="请选择入库人" clearable filterable>
                <el-option v-for="user in user_list" :key="user.id" :label="user.name" :value="user.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="出库数：" prop="number_min">
              <el-input v-model="searchForm.number_min" placeholder="请输入出库数量最小值" clearable type="number"
                        step="1"
              >
              </el-input>
            </el-form-item>
            <el-form-item label="至" prop="number_max">
              <el-input v-model="searchForm.number_max" placeholder="请输入出库数量最大值" clearable type="number"
                        step="1"
              >
              </el-input>
            </el-form-item>

          </template>
          <template slot="searchBtn">
            <el-form-item>
              <el-button type="primary" @click="search">查询</el-button>
              <el-button @click="resetForm('searchForm')">重置</el-button>
            </el-form-item>
          </template>
        </search-form-box>
      </el-form>
    </div>
    <div class="table-box">
      <el-button size="mini" type="primary" style="margin-bottom: 10px" @click="showAdd">新增出库</el-button>
      <el-table v-loading="tableLoading" :data="tableData" size="mini" border style="width: 100%">
        <el-table-column type="index" label="序号" width="50" align="center">
          <template slot-scope="scope">
            <span>{{ (listParams.page - 1) * listParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="good_name" label="物品名称" width="200" align="center"></el-table-column>
        <el-table-column prop="number" label="数量" width="100" align="center"></el-table-column>
        <el-table-column prop="user_name" label="出库人" min-width="200" align="center"></el-table-column>
        <el-table-column prop="created_at" label="出库时间" min-width="200" align="center"></el-table-column>

        <el-table-column label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <div class="operation">
              <el-button-group>
                <el-button
                  type="primary"
                  icon="el-icon-edit-outline"
                  size="mini"
                  @click.native="showEdit(scope.row)"
                >编辑
                </el-button>
              </el-button-group>
            </div>
          </template>
        </el-table-column>

      </el-table>
    </div>
    <el-pagination :current-page.sync="searchForm.page" :page-sizes="[10,20,50,100, 200,400]"
                   layout="total, sizes, prev, pager, next, jumper"
                   :page-size.sync="searchForm.page_size" :total="total"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange">
    </el-pagination>
    <goods-out-dialog ref="goodsOutDialog" @reload="reload"></goods-out-dialog>
  </div>
</template>

<script>
import SearchFormBox from '@/components/SearchFormBox'
import { list } from '@/api/goods/goods_out'
import { allList } from '@/api/system/user'
import { list as typeList } from '@/api/goods/goods_type'
import GoodsOutDialog from '@/views/administration/goods/components/goodsOutDialog.vue'

export default {
  name: 'GoodsOut',
  components: { GoodsOutDialog, SearchFormBox },
  data() {
    return {
      listParams: {
        goods_type_id: null,
        user_id: null,
        number_min: null,
        number_max: null,
        page: 1,
        page_size: 10
      },
      searchForm: {
        goods_type_id: null,
        user_id: null,
        number_min: null,
        number_max: null,
        page: 1,
        page_size: 10
      },
      tableData: [],
      tableLoading: false,
      total: null,
      user_list: [],
      goods_type_arr: []
    }
  },
  created() {
    this.getList()
    this.initUserList()
    this.initGoodsType()
  },
  methods: {
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
    getList() {
      this.tableLoading = true
      this.listParams = Object.assign({}, this.searchForm)
      list(this.listParams).then(res => {
        if (res.code === 200) {
          this.tableLoading = false
          this.tableData = res.data.records
          this.total = res.data.total
        }
      }).catch(() => {
        this.tableData = []
        this.tableLoading = false
      })
    },
    search() {
      this.getList()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.getList()
    },
    showAdd() {
      this.$refs.goodsOutDialog.show()
    },
    // 编辑弹出框
    showEdit(row) {
      this.$refs.goodsOutDialog.goodOut =  Object.assign({goods_type_name: row.goods_name}, row)
      this.$refs.goodsOutDialog.show()
    },
    initUserList() {
      allList().then(res => {
        this.user_list = res.data.filter(user => user.status)
      }).catch(() => {

      })
    },
    initGoodsType() {
      typeList().then(res => {
        this.goods_type_arr = res.data
      }).catch(() => {

      })
    },
    reload() {
      this.initGoodsType()
      this.getList()
    }
  }
}
</script>

<style scoped>
.page_main {
  padding: 20px;
}

.table-box {
  margin-bottom: 20px;
}
</style>
